home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / program / 317 / asmsrc / write.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-10-20  |  28.3 KB  |  904 lines

  1. /* write.c - emit .o file - Copyright(C)1986 Free Software Foundation, Inc. */
  2. /* Copyright (C) 1986,1987 Free Software Foundation, Inc.
  3.  
  4. This file is part of Gas, the GNU Assembler.
  5.  
  6. The GNU assembler is distributed in the hope that it will be
  7. useful, but WITHOUT ANY WARRANTY.  No author or distributor
  8. accepts responsibility to anyone for the consequences of using it
  9. or for whether it serves any particular purpose or works at all,
  10. unless he says so in writing.  Refer to the GNU Assembler General
  11. Public License for full details.
  12.  
  13. Everyone is granted permission to copy, modify and redistribute
  14. the GNU Assembler, but only under the conditions described in the
  15. GNU Assembler General Public License.  A copy of this license is
  16. supposed to have been given to you along with the GNU Assembler
  17. so you can know your rights and responsibilities.  It should be
  18. in a file named COPYING.  Among other things, the copyright
  19. notice and this notice must be preserved on all copies.  */
  20.  
  21. /* 
  22.  
  23.    Umm, with real good luck, this thing should be set up to do byteordering
  24.    correctly, but I may have managed to miss a place or two.  Treat a.out
  25.    very carefully until you're SURE that it works. . .
  26.  
  27.    In order to cross-assemble the target machine must have an a.out header
  28.    similar to the one in a.out.h on THIS machine.  Byteorder doesn't matter;
  29.    we take special care of it, but the numbers must be the same SIZE (# of
  30.    bytes) and in the same PLACE.  If this is not true, you will have some
  31.    trouble.
  32.  */
  33.  
  34. #include "as.h"
  35. #include "md.h"
  36. #include "subsegs.h"
  37. #include "obstack.h"
  38. #include "struc-symbol.h"
  39. #include "write.h"
  40. #include "symbols.h"
  41.  
  42. void    append();
  43.  
  44. #ifdef hpux
  45. #define EXEC_MACHINE_TYPE (0x20C)
  46. #endif
  47.  
  48. /*
  49.  * In: length of relocation (or of address) in chars: 1, 2 or 4.
  50.  * Out: GNU LD relocation length code: 0, 1, or 2.
  51.  */
  52.  
  53. static unsigned char
  54.  
  55. nbytes_r_length [] = {
  56.   42, 0, 1, 42, 2
  57.   };
  58.  
  59.  
  60. static struct frag *    text_frag_root;
  61. static struct frag *    data_frag_root;
  62.  
  63. static struct frag *    text_last_frag;    /* Last frag in segment. */
  64. static struct frag *    data_last_frag;    /* Last frag in segment. */
  65.  
  66. static struct exec    the_exec;
  67.  
  68. static long int string_byte_count;
  69.  
  70. static char *        the_object_file;
  71.  
  72. static char *        next_object_file_charP;    /* Tracks object file bytes. */
  73.  
  74. static long int        size_of_the_object_file; /* # bytes in object file. */
  75.  
  76. /* static long int        length; JF unused */    /* String length, including trailing '\0'. */
  77.  
  78. void        relax_segment();
  79. void        emit_segment();
  80. relax_addressT    relax_align();
  81. long int    fixup_segment();
  82. void        emit_relocations();
  83. /*
  84.  *            fix_new()
  85.  *
  86.  * Create a fixS in obstack 'notes'.
  87.  */
  88. void
  89. fix_new (frag, where, size, add_symbol, sub_symbol, offset, pcrel)
  90.      fragS *    frag;        /* Which frag? */
  91.      int    where;        /* Where in that frag? */
  92.      short int    size;        /* 1, 2  or 4 usually. */
  93.      symbolS *    add_symbol;    /* X_add_symbol. */
  94.      symbolS *    sub_symbol;    /* X_subtract_symbol. */
  95.      long int    offset;        /* X_add_number. */
  96.      int    pcrel;        /* TRUE if PC-relative relocation. */
  97. {
  98.   register fixS *    fixP;
  99.  
  100.   fixP = (fixS *)obstack_alloc(¬es,sizeof(fixS));
  101.  
  102.   fixP -> fx_frag    = frag;
  103.   fixP -> fx_where    = where;
  104.   fixP -> fx_size    = size;
  105.   fixP -> fx_addsy    = add_symbol;
  106.   fixP -> fx_subsy    = sub_symbol;
  107.   fixP -> fx_offset    = offset;
  108.   fixP -> fx_pcrel    = pcrel;
  109.   fixP -> fx_next    = * seg_fix_rootP;
  110.  
  111.   * seg_fix_rootP = fixP;
  112. }
  113.  
  114. void
  115. write_object_file()
  116. {
  117.   register struct frchain *    frchainP; /* Track along all frchains. */
  118.   register fragS *        fragP;    /* Track along all frags. */
  119.   register struct frchain *    next_frchainP;
  120.   register fragS * *        prev_fragPP;
  121.   register char *        name;
  122.   register symbolS *        symbolP;
  123.   register symbolS **        symbolPP;
  124.   /* register fixS *        fixP; JF unused */
  125.   unsigned
  126.       text_siz,
  127.     data_siz,
  128.     syms_siz,
  129.     tr_siz,
  130.     dr_siz;
  131.   void output_file_create();
  132.   void output_file_append();
  133.   void output_file_close();
  134.   void gdb_emit();
  135.   void gdb_end();
  136.   extern long omagic;        /* JF magic # to write out.  Is different for
  137.                    Suns and Vaxen and other boxes */
  138.  
  139.   /*
  140.    * After every sub-segment, we fake an ".align ...". This conforms to BSD4.2
  141.    * brane-damage. We then fake ".fill 0" because that is the kind of frag
  142.    * that requires least thought. ".align" frags like to have a following
  143.    * frag since that makes calculating their intended length trivial.
  144.    */
  145. #define SUB_SEGMENT_ALIGN (2)
  146.   for ( frchainP=frchain_root; frchainP; frchainP=frchainP->frch_next )
  147.     {
  148.       subseg_new (frchainP -> frch_seg, frchainP -> frch_subseg);
  149.       frag_align (SUB_SEGMENT_ALIGN, 0);
  150.                 /* frag_align will have left a new frag. */
  151.                 /* Use this last frag for an empty ".fill". */
  152.       /*
  153.        * For this segment ...
  154.        * Create a last frag. Do not leave a "being filled in frag".
  155.        */
  156.       frag_wane (frag_now);
  157.       frag_now -> fr_fix    = 0;
  158.       know( frag_now -> fr_next == NULL );
  159.       /* know( frags . obstack_c_base == frags . obstack_c_next_free ); */
  160.       /* Above shows we haven't left a half-completed object on obstack. */
  161.     }
  162.  
  163.   /*
  164.    * From now on, we don't care about sub-segments.
  165.    * Build one frag chain for each segment. Linked thru fr_next.
  166.    * We know that there is at least 1 text frchain & at least 1 data frchain.
  167.    */
  168.   prev_fragPP = &text_frag_root;
  169.   for ( frchainP=frchain_root; frchainP; frchainP=next_frchainP )
  170.     {
  171.       know( frchainP -> frch_root );
  172.       * prev_fragPP = frchainP -> frch_root;
  173.       prev_fragPP = & frchainP -> frch_last -> fr_next;
  174.       if (   ((next_frchainP = frchainP->frch_next) == NULL)
  175.       || next_frchainP == data0_frchainP)
  176.     {
  177.       prev_fragPP = & data_frag_root;
  178.       if ( next_frchainP )
  179.         {
  180.           text_last_frag = frchainP -> frch_last;
  181.         }
  182.       else
  183.         {
  184.           data_last_frag = frchainP -> frch_last;
  185.         }
  186.     }
  187.     }                /* for(each struct frchain) */
  188.  
  189.   /*
  190.    * We have two segments. If user gave -R flag, then we must put the
  191.    * data frags into the text segment. Do this before relaxing so
  192.    * we know to take advantage of -R and make shorter addresses.
  193.    */
  194.   if ( flagseen [ 'R' ] )
  195.     {
  196.       fixS *tmp;
  197.  
  198.       text_last_frag -> fr_next = data_frag_root;
  199.       text_last_frag = data_last_frag;
  200.       data_last_frag = NULL;
  201.       data_frag_root = NULL;
  202.       if(text_fix_root) {
  203.     for(tmp=text_fix_root;tmp->fx_next;tmp=tmp->fx_next)
  204.       ;
  205.     tmp->fx_next=data_fix_root;
  206.       } else
  207.         text_fix_root=data_fix_root;
  208.       data_fix_root=NULL;
  209.     }
  210.  
  211.   relax_segment (text_frag_root, SEG_TEXT);
  212.   relax_segment (data_frag_root, SEG_DATA);
  213.   /*
  214.    * Now the addresses of frags are correct within the segment.
  215.    */
  216.  
  217.  know(   text_last_frag -> fr_type   == rs_fill && text_last_frag -> fr_offset == 0        );
  218.   text_siz=text_last_frag->fr_address;
  219.   md_number_to_chars((char *)&the_exec.a_text,text_siz, sizeof(the_exec.a_text));
  220.   /* the_exec . a_text = text_last_frag -> fr_address; */
  221.  
  222.   /*
  223.    * Join the 2 segments into 1 huge segment.
  224.    * To do this, re-compute every rn_address in the SEG_DATA frags.
  225.    * Then join the data frags after the text frags.
  226.    *
  227.    * Determine a_data [length of data segment].
  228.    */
  229.   if (data_frag_root)
  230.     {
  231.       register relax_addressT    slide;
  232.  
  233.       know(   text_last_frag -> fr_type   == rs_fill && text_last_frag -> fr_offset == 0 );
  234.       data_siz=data_last_frag->fr_address;
  235.       md_number_to_chars((char *)&the_exec.a_data,data_siz,sizeof(the_exec.a_data));
  236.       /* the_exec . a_data = data_last_frag -> fr_address; */
  237.       slide = text_siz; /* Address in file of the data segment. */
  238.       for (fragP = data_frag_root;
  239.        fragP;
  240.        fragP = fragP -> fr_next)
  241.     {
  242.       fragP -> fr_address += slide;
  243.     }
  244.       know( text_last_frag );
  245.       text_last_frag -> fr_next = data_frag_root;
  246.     }
  247.   else {
  248.       md_number_to_chars((char *)&the_exec.a_data,0,sizeof(the_exec.a_data));
  249.       data_siz = 0;
  250.   }
  251.  
  252.   bss_address_frag . fr_address = text_siz + data_siz;
  253.  
  254.   md_number_to_chars((char *)&the_exec.a_bss,local_bss_counter,sizeof(the_exec.a_bss));
  255.  
  256.           
  257.   /*
  258.    *
  259.    * Crawl the symbol chain.
  260.    *
  261.    * For each symbol whose value depends on a frag, take the address of
  262.    * that frag and subsume it into the value of the symbol.
  263.    * After this, there is just one way to lookup a symbol value.
  264.    * Values are left in their final state for object file emission.
  265.    * We adjust the values of 'L' local symbols, even if we do
  266.    * not intend to emit them to the object file, because their values
  267.    * are needed for fix-ups.
  268.    *
  269.    * Unless we saw a -L flag, remove all symbols that begin with 'L'
  270.    * from the symbol chain.
  271.    *
  272.    * Count the (length of the nlists of the) (remaining) symbols.
  273.    * Assign a symbol number to each symbol.
  274.    * Count the number of string-table chars we will emit.
  275.    *
  276.    */
  277.   know( zero_address_frag . fr_address == 0 );
  278.   string_byte_count = sizeof( string_byte_count );
  279.  
  280.   /* JF deal with forward references first. . . */
  281.   for(symbolP=symbol_rootP;symbolP;symbolP=symbolP->sy_next) {
  282.       if(symbolP->sy_forward) {
  283.         symbolP->sy_value+=symbolP->sy_forward->sy_value+symbolP->sy_forward->sy_frag->fr_address;
  284.         symbolP->sy_forward=0;
  285.     }
  286.   }
  287.   symbolPP = & symbol_rootP;    /* -> last symbol chain link. */
  288.   {
  289.     register long int        symbol_number;
  290.  
  291.     symbol_number = 0;
  292.     while (symbolP  = * symbolPP)
  293.       {
  294.     name = symbolP -> sy_name;
  295.     if(flagseen['R'] && (symbolP->sy_nlist.n_type&N_DATA)) {
  296.       symbolP->sy_nlist.n_type&= ~N_DATA;
  297.       symbolP->sy_nlist.n_type|= N_TEXT;
  298.     }
  299.     /* if(symbolP->sy_forward) {
  300.       symbolP->sy_value += symbolP->sy_forward->sy_value + symbolP->sy_forward->sy_frag->fr_address;
  301.     } */
  302.     
  303.     symbolP -> sy_value += symbolP -> sy_frag -> fr_address;
  304.         /* JF the 128 bit is a hack so stabs like
  305.            "LET_STMT:23. . ."  don't go away */
  306.     /* CPH: 128 bit hack is moby loser.  N_SO for file "Lower.c"
  307.        fell through the cracks.  I think that N_STAB should be
  308.        used instead of 128. */
  309.         /* JF the \001 bit is to make sure that local labels
  310.            ( 1: - 9: don't make it into the symtable either */
  311.     if ( !name || (symbolP->sy_nlist.n_type&N_STAB)
  312.         || (name[0]!='\001' && (flagseen ['L'] || name [0] != 'L' )))
  313.       {
  314.         symbolP -> sy_number = symbol_number ++;
  315.         if (name)
  316.           {            /* Ordinary case. */
  317.         symbolP -> sy_name_offset = string_byte_count;
  318.         string_byte_count += strlen (symbolP  -> sy_name) + 1;
  319.           }
  320.         else            /* .Stabd case. */
  321.         symbolP -> sy_name_offset = 0;
  322.         symbolPP = & (symbolP -> sy_next);
  323.       }
  324.     else
  325.         * symbolPP = symbolP -> sy_next;
  326.       }                /* for each symbol */
  327.  
  328.     syms_siz = sizeof( struct nlist) * symbol_number;
  329.     md_number_to_chars((char *)&the_exec.a_syms,syms_siz,sizeof(the_exec.a_syms));
  330.     /* the_exec . a_syms = sizeof( struct nlist) * symbol_number; */
  331.   }
  332.  
  333.   /*
  334.    * Addresses of frags now reflect addresses we use in the object file.
  335.    * Symbol values are correct.
  336.    * Scan the frags, converting any ".org"s and ".align"s to ".fill"s.
  337.    * Also converting any machine-dependent frags using md_convert_frag();
  338.    */
  339.   for (fragP = text_frag_root;  fragP;  fragP = fragP -> fr_next)
  340.     {
  341.       switch (fragP -> fr_type)
  342.     {
  343.     case rs_align:
  344.     case rs_org:
  345.       fragP -> fr_type = rs_fill;
  346.       know( fragP -> fr_var == 1 );
  347.       know( fragP -> fr_next );
  348.       fragP -> fr_offset
  349.         =     fragP -> fr_next -> fr_address
  350.           -   fragP -> fr_address
  351.         - fragP -> fr_fix;
  352.       break;
  353.  
  354.     case rs_fill:
  355.       break;
  356.  
  357.     case rs_machine_dependent:
  358.       md_convert_frag (fragP);
  359.       /*
  360.        * After md_convert_frag, we make the frag into a ".space 0".
  361.        * Md_convert_frag() should set up any fixSs and constants
  362.        * required.
  363.        */
  364.       frag_wane (fragP);
  365.       break;
  366.  
  367.     default:
  368.       BAD_CASE( fragP -> fr_type );
  369.       break;
  370.     }            /* switch (fr_type) */
  371.     }                /* for each frag. */
  372.  
  373.   /*
  374.    * Scan every FixS performing fixups. We had to wait until now to do
  375.    * this because md_convert_frag() may have made some fixSs.
  376.    */
  377.   /* the_exec . a_trsize
  378.     = sizeof(struct relocation_info) * fixup_segment (text_fix_root, N_TEXT);
  379.   the_exec . a_drsize
  380.     = sizeof(struct relocation_info) * fixup_segment (data_fix_root, N_DATA); */
  381.  
  382.   tr_siz=sizeof(struct relocation_info) * fixup_segment (text_fix_root, N_TEXT);
  383.   md_number_to_chars((char *)&the_exec.a_trsize, tr_siz ,sizeof(the_exec.a_trsize));
  384.   dr_siz=sizeof(struct relocation_info) * fixup_segment (data_fix_root, N_DATA);
  385.   md_number_to_chars((char *)&the_exec.a_drsize, dr_siz, sizeof(the_exec.a_drsize));
  386. #ifdef EXEC_MACHINE_TYPE
  387.   md_number_to_chars((char *)&the_exec.a_machtype, EXEC_MACHINE_TYPE, sizeof(the_exec.a_machtype));
  388. #endif
  389.  
  390.   md_number_to_chars((char *)&the_exec.a_magic,omagic,sizeof(the_exec.a_magic));
  391.   /* the_exec . a_magic    = omagic;    /* Always for rel files. */
  392.   md_number_to_chars((char *)&the_exec.a_entry,0,sizeof(the_exec.a_entry));
  393.   /* the_exec . a_entry = 0; */
  394.  
  395.   size_of_the_object_file =
  396.     sizeof( the_exec ) +
  397.       text_siz +
  398.         data_siz +
  399.       syms_siz +
  400.         tr_siz +
  401.           dr_siz +
  402.         string_byte_count;
  403.     
  404.   next_object_file_charP
  405.     = the_object_file
  406.       = xmalloc ( size_of_the_object_file );
  407.  
  408.   output_file_create (out_file_name);
  409.  
  410.   append (& next_object_file_charP, (char *)(&the_exec), (unsigned long)sizeof(the_exec));
  411.  
  412.   /*
  413.    * Emit code.
  414.    */
  415.   for (fragP = text_frag_root;  fragP;  fragP = fragP -> fr_next)
  416.     {
  417.       register long int        count;
  418.       register char *        fill_literal;
  419.       register long int        fill_size;
  420.  
  421.       know( fragP -> fr_type == rs_fill );
  422.       append (& next_object_file_charP, fragP -> fr_literal, (unsigned long)fragP -> fr_fix);
  423.       fill_literal= fragP -> fr_literal + fragP -> fr_fix;
  424.       fill_size   = fragP -> fr_var;
  425.       know( fragP -> fr_offset >= 0 );
  426.       for (count = fragP -> fr_offset;  count;  count --)
  427.       append (& next_object_file_charP, fill_literal, (unsigned long)fill_size);
  428.     }                /* for each code frag. */
  429.  
  430.   /*
  431.    * Emit relocations.
  432.    */
  433.   emit_relocations (text_fix_root, (relax_addressT)0);
  434.   emit_relocations (data_fix_root, text_last_frag -> fr_address);
  435.  
  436.   /*
  437.    * Emit all symbols left in the symbol chain.
  438.    * Any symbol still undefined is made N_EXT.
  439.    */
  440.   for (   symbolP = symbol_rootP;   symbolP;   symbolP = symbolP -> sy_next   )
  441.     {
  442.       register char *    temp;
  443.  
  444.       temp = symbolP -> sy_nlist . n_un . n_name;
  445.       /* JF fix the numbers up. Call by value RULES! */
  446.       md_number_to_chars((char *)&(symbolP -> sy_nlist  . n_un . n_strx ),symbolP -> sy_name_offset,sizeof(symbolP -> sy_nlist  . n_un . n_strx ));
  447.       md_number_to_chars((char *)&(symbolP->sy_nlist.n_desc),symbolP->sy_nlist.n_desc,sizeof(symbolP -> sy_nlist  . n_desc));
  448.       md_number_to_chars((char *)&(symbolP->sy_nlist.n_value),symbolP->sy_nlist.n_value,sizeof(symbolP->sy_nlist.n_value));
  449.       /* symbolP -> sy_nlist  . n_un . n_strx = symbolP -> sy_name_offset; JF replaced by md above */
  450.       if (symbolP -> sy_type == N_UNDF)
  451.       symbolP -> sy_type |= N_EXT; /* Any undefined symbols become N_EXT. */
  452.       append (& next_object_file_charP, (char *)(& symbolP -> sy_nlist),
  453.           (unsigned long)sizeof(struct nlist));
  454.       symbolP -> sy_nlist . n_un . n_name = temp;
  455.     }                /* for each symbol */
  456.  
  457.   /*
  458.    * next_object_file_charP -> slot for next object byte.
  459.    * Emit strings.
  460.    * Find strings by crawling along symbol table chain.
  461.    */
  462.   append (& next_object_file_charP, (char *)&string_byte_count, (unsigned long)sizeof(string_byte_count));
  463.   for (   symbolP = symbol_rootP;   symbolP;   symbolP = symbolP -> sy_next   )
  464.     {
  465.       if (symbolP -> sy_name)
  466.     {            /* Ordinary case: not .stabd. */
  467.       append (& next_object_file_charP, symbolP -> sy_name,
  468.           (unsigned long)(strlen (symbolP -> sy_name) + 1));
  469.     }
  470.     }                /* for each symbol */
  471.  
  472.   know( next_object_file_charP == the_object_file + size_of_the_object_file );
  473.  
  474.   output_file_append (the_object_file, size_of_the_object_file, out_file_name);
  475.  
  476.   if (flagseen['G'])        /* GDB symbol file to be appended? */
  477.     {
  478.       gdb_emit (out_file_name);
  479.       gdb_end ();
  480.     }
  481.  
  482.   output_file_close (out_file_name);
  483. }                /* write_object_file() */
  484.  
  485. /*
  486.  *            relax_segment()
  487.  *
  488.  * Now we have a segment, not a crowd of sub-segments, we can make fr_address
  489.  * values.
  490.  *
  491.  * Relax the frags.
  492.  *
  493.  * After this, all frags in this segment have addresses that are correct
  494.  * within the segment. Since segments live in different file addresses,
  495.  * these frag addresses may not be the same as final object-file addresses.
  496.  */
  497. static void
  498. relax_segment (segment_frag_root, segment_type)
  499.      struct frag *    segment_frag_root;
  500.      segT        segment_type; /* N_DATA or N_TEXT */
  501. {
  502.   register struct frag *    fragP;
  503.   register relax_addressT    address;
  504.   /* register relax_addressT    old_address; JF unused */
  505.   /* register relax_addressT    new_address; JF unused */
  506.  
  507.   know( segment_type == SEG_DATA || segment_type == SEG_TEXT );
  508.  
  509.   /* In case md_estimate_size_before_relax() wants to make fixSs. */
  510.   subseg_change (segment_type, 0);
  511.  
  512.   /*
  513.    * For each frag in segment: count and store  (a 1st guess of) fr_address.
  514.    */
  515.   address = 0;
  516.   for ( fragP = segment_frag_root;   fragP;   fragP = fragP -> fr_next )
  517.     {
  518.       fragP -> fr_address = address;
  519.       address += fragP -> fr_fix;
  520.       switch (fragP -> fr_type)
  521.     {
  522.     case rs_fill:
  523.       address += fragP -> fr_offset * fragP -> fr_var;
  524.       break;
  525.  
  526.     case rs_align:
  527.       address += relax_align (address, fragP -> fr_offset);
  528.       break;
  529.  
  530.     case rs_org:
  531.       /*
  532.        * Assume .org is nugatory. It will grow with 1st relax.
  533.        */
  534.       break;
  535.  
  536.     case rs_machine_dependent:
  537.       address += md_estimate_size_before_relax
  538.         (fragP, seg_N_TYPE [(int) segment_type]);
  539.       break;
  540.  
  541.     default:
  542.       BAD_CASE( fragP -> fr_type );
  543.       break;
  544.     }            /* switch(fr_type) */
  545.     }                /* for each frag in the segment */
  546.  
  547.   /*
  548.    * Do relax().
  549.    */
  550.   {
  551.     register long int    stretch; /* May be any size, 0 or negative. */
  552.                 /* Cumulative number of addresses we have */
  553.                 /* relaxed this pass. */
  554.                 /* We may have relaxed more than one address. */
  555.     register long int stretched;  /* Have we stretched on this pass? */
  556.  
  557.     do
  558.       {
  559.     stretch = stretched = 0;
  560.     for (fragP = segment_frag_root;  fragP;  fragP = fragP -> fr_next)
  561.       {
  562.         register long int    growth;
  563.         register long int    was_address;
  564.         /* register long int    var; */
  565.         register long int    offset;
  566.         register symbolS *    symbolP;
  567.         register long int    target;
  568.         register long int    after;
  569.         register long int    aim;
  570.  
  571.         was_address = fragP -> fr_address;
  572.         address = fragP -> fr_address += stretch;
  573.         symbolP = fragP -> fr_symbol;
  574.         offset = fragP -> fr_offset;
  575.         /* var = fragP -> fr_var; */
  576.         switch (fragP -> fr_type)
  577.           {
  578.           case rs_fill:    /* .fill never relaxes. */
  579.         growth = 0;
  580.         break;
  581.  
  582.           case rs_align:
  583.         growth = relax_align ((relax_addressT)(address + fragP -> fr_fix), offset)
  584.           - relax_align ((relax_addressT)(was_address +  fragP -> fr_fix), offset);
  585.         break;
  586.  
  587.           case rs_org:
  588.         target = offset;
  589.         if (symbolP)
  590.           {
  591.             know(   ((symbolP -> sy_type & N_TYPE) == N_ABS) || ((symbolP -> sy_type & N_TYPE) == N_DATA) || ((symbolP -> sy_type & N_TYPE) == N_TEXT));
  592.             know( symbolP -> sy_frag );
  593.             know( (symbolP->sy_type&N_TYPE)!=N_ABS || symbolP->sy_frag==&zero_address_frag );
  594.             target +=
  595.               symbolP -> sy_value
  596.             + symbolP -> sy_frag -> fr_address;
  597.           }
  598.         know( fragP -> fr_next );
  599.         after = fragP -> fr_next -> fr_address;
  600.         growth = (target <= address + fragP -> fr_fix) ? 0 : target - after;
  601.                 /* Growth may be -ve, but variable part */
  602.                 /* of frag cannot have < 0 chars. */
  603.                 /* That is, we can't .org backwards. */
  604.         break;
  605.  
  606.           case rs_machine_dependent:
  607.         {
  608.           register relax_typeS *    this_type;
  609.           register relax_typeS *    start_type;
  610.           register relax_substateT    next_state;
  611.           register relax_substateT    this_state;
  612.  
  613.           start_type = this_type
  614.             = md_relax_table + (this_state = fragP -> fr_subtype);
  615.         target = offset;
  616.         if (symbolP)
  617.           {
  618.  know(   ((symbolP -> sy_type & N_TYPE) == N_ABS) || ((symbolP -> sy_type &
  619.  N_TYPE) == N_DATA) || ((symbolP -> sy_type & N_TYPE) == N_TEXT));
  620.             know( symbolP -> sy_frag );
  621.             know( (symbolP->sy_type&N_TYPE)!=N_ABS || symbolP->sy_frag==&zero_address_frag );
  622.             target +=
  623.               symbolP -> sy_value
  624.             + symbolP -> sy_frag -> fr_address;
  625.  
  626.             /* If frag has yet to be reached on this pass,
  627.                assume it will move by STRETCH just as we did.
  628.                If this is not so, it will be because some frag
  629.                between grows, and that will force another pass.  */
  630.  
  631.                    /* JF was just address */
  632.                 /* JF also added isdnrange hack */
  633.                 /* There's gotta be a better/faster/etc way
  634.                    to do this. . . */
  635.             if (symbolP->sy_frag->fr_address > was_address && isdnrange(fragP,symbolP->sy_frag))
  636.               target += stretch;
  637.  
  638.           }
  639.           aim = target - address - fragP -> fr_fix;
  640.           if (aim < 0)
  641.             {
  642.               /* Look backwards. */
  643.               for (next_state = this_type -> rlx_more;  next_state;  )
  644.             {
  645.               if (aim >= this_type -> rlx_backward)
  646.                   next_state = 0;
  647.               else
  648.                 {    /* Grow to next state. */
  649.                   this_type = md_relax_table + (this_state = next_state);
  650.                   next_state = this_type -> rlx_more;
  651.                 }
  652.             }
  653.             }
  654.           else
  655.             {
  656. #ifdef DONTDEF
  657. /* JF these next few lines of code are for the mc68020 which can't handle short
  658.    offsets of zero in branch instructions.  What a kludge! */
  659.  if(aim==0 && this_state==(1<<2+0)) {    /* FOO hard encoded from m.c */
  660.     aim=this_type->rlx_forward+1;    /* Force relaxation into word mode */
  661.  }
  662. #endif
  663. /* JF end of 68020 code */
  664.               /* Look forwards. */
  665.               for (next_state = this_type -> rlx_more;  next_state;  )
  666.             {
  667.               if (aim <= this_type -> rlx_forward)
  668.                   next_state = 0;
  669.               else
  670.                 {    /* Grow to next state. */
  671.                   this_type = md_relax_table + (this_state = next_state);
  672.                   next_state = this_type -> rlx_more;
  673.                 }
  674.             }
  675.             }
  676.           if (growth = this_type -> rlx_length - start_type -> rlx_length)
  677.               fragP -> fr_subtype = this_state;
  678.         }
  679.         break;
  680.  
  681.           default:
  682.         BAD_CASE( fragP -> fr_type );
  683.         break;
  684.           }
  685.         if(growth) {
  686.           stretch += growth;
  687.           stretched++;
  688.         }
  689.       }            /* For each frag in the segment. */
  690.       } while (stretched);    /* Until nothing further to relax. */
  691.   }
  692.  
  693.   /*
  694.    * We now have valid fr_address'es for each frag.
  695.    */
  696.  
  697.   /*
  698.    * All fr_address's are correct, relative to their own segment.
  699.    * We have made all the fixS we will ever make.
  700.    */
  701. }                /* relax_segment() */
  702.  
  703. /*
  704.  * Relax_align. Advance location counter to next address that has 'alignment'
  705.  * lowest order bits all 0s.
  706.  */
  707.  
  708. static relax_addressT        /* How many addresses does the .align take? */
  709. relax_align (address, alignment)
  710.      register relax_addressT    address; /* Address now. */
  711.      register long int        alignment; /* Alignment (binary). */
  712. {
  713.   relax_addressT    mask;
  714.   relax_addressT    new_address;
  715.  
  716.   mask = ~ ( (~0) << alignment );
  717.   new_address = (address + mask) & (~ mask);
  718.   return (new_address - address);
  719. }
  720.  
  721. /*
  722.  *            fixup_segment()
  723.  */
  724. static long int
  725. fixup_segment (fixP, this_segment_type)
  726.      register fixS *    fixP;
  727.      int        this_segment_type; /* N_TYPE bits for segment. */
  728. {
  729.   register long int        seg_reloc_count;
  730.         /* JF these all used to be local to the for loop, but GDB doesn't seem to be able to deal with them there, so I moved them here for a bit. */
  731.       register symbolS *    add_symbolP;
  732.       register symbolS *    sub_symbolP;
  733.       register long int        add_number;
  734.       register int        size;
  735.       register char *        place;
  736.       register long int        where;
  737.       register char        pcrel;
  738.       register fragS *        fragP;
  739.       register int        add_symbol_N_TYPE;
  740.  
  741.  
  742.   seg_reloc_count = 0;
  743.   for ( ;  fixP;  fixP = fixP -> fx_next)
  744.     {
  745.       fragP       = fixP  -> fx_frag;
  746.       know( fragP );
  747.       where      = fixP  -> fx_where;
  748.       place       = fragP -> fr_literal + where;
  749.       size      = fixP  -> fx_size;
  750.       add_symbolP = fixP  -> fx_addsy;
  751.       sub_symbolP = fixP  -> fx_subsy;
  752.       add_number  = fixP  -> fx_offset;
  753.       pcrel      = fixP  -> fx_pcrel;
  754.       if(add_symbolP)
  755.     add_symbol_N_TYPE = add_symbolP -> sy_type & N_TYPE;
  756.       if (sub_symbolP)
  757.     {
  758.       if(!add_symbolP)    /* Its just -sym */
  759.         {
  760.           if(sub_symbolP->sy_type!=N_ABS)
  761.             as_warn("Negative of non-absolute symbol %s", sub_symbolP->sy_name);
  762.           add_number-=sub_symbolP->sy_value;
  763.         }
  764.       else if (   ((sub_symbolP -> sy_type ^ add_symbol_N_TYPE) & N_TYPE) == 0
  765.           && (   add_symbol_N_TYPE == N_DATA
  766.           || add_symbol_N_TYPE == N_TEXT
  767.           || add_symbol_N_TYPE == N_BSS
  768.           || add_symbol_N_TYPE == N_ABS))
  769.         {
  770.           /* Difference of 2 symbols from same segment. */
  771.           /* Can't make difference of 2 undefineds: 'value' means */
  772.           /* something different for N_UNDF. */
  773.           add_number += add_symbolP -> sy_value - sub_symbolP -> sy_value;
  774.           add_symbolP = NULL;
  775.           fixP -> fx_addsy = NULL;
  776.         }
  777.       else
  778.         {
  779.           /* Different segments in subtraction. */
  780.           know( sub_symbolP -> sy_type != (N_ABS | N_EXT))
  781.         if (sub_symbolP -> sy_type == N_ABS)
  782.             add_number -= sub_symbolP -> sy_value;
  783.         else
  784.           {
  785.             as_warn("Can't emit reloc {- %s-seg symbol \"%s\"} @ file address %d.",
  786.                 seg_name[(int)N_TYPE_seg[sub_symbolP->sy_type&N_TYPE]],
  787.                 sub_symbolP -> sy_name, fragP -> fr_address + where);
  788.           }
  789.         }
  790.     }
  791.       if (add_symbolP)
  792.     {
  793.       if (add_symbol_N_TYPE == this_segment_type && pcrel)
  794.         {
  795.           /*
  796.            * This fixup was made when the symbol's segment was
  797.            * SEG_UNKNOWN, but it is now in the local segment.
  798.            * So we know how to do the address without relocation.
  799.            */
  800.           add_number += add_symbolP -> sy_value;
  801.           add_number -= size + where + fragP -> fr_address;
  802.           pcrel = 0;    /* Lie. Don't want further pcrel processing. */
  803.           fixP -> fx_addsy = NULL; /* No relocations please. */
  804.           /*
  805.            * It would be nice to check that the address does not overflow.
  806.            * I didn't do this check because:
  807.            * +  It is machine dependent in the general case (eg 32032)
  808.            * +  Compiler output will never need this checking, so why
  809.            *    slow down the usual case?
  810.            */
  811.         }
  812.       else
  813.         {
  814.           switch (add_symbol_N_TYPE)
  815.         {
  816.         case N_ABS:
  817.           add_number += add_symbolP -> sy_value;
  818.           fixP -> fx_addsy = NULL;
  819.           add_symbolP = NULL;
  820.           break;
  821.           
  822.         case N_BSS:
  823.         case N_DATA:
  824.         case N_TEXT:
  825.           seg_reloc_count ++;
  826.           add_number += add_symbolP -> sy_value;
  827.           break;
  828.           
  829.         case N_UNDF:
  830.           seg_reloc_count ++;
  831.           break;
  832.           
  833.         default:
  834.           BAD_CASE( add_symbol_N_TYPE );
  835.           break;
  836.         }        /* switch on symbol seg */
  837.         }            /* if not in local seg */
  838.     }            /* if there was a + symbol */
  839.       if (pcrel)
  840.     {
  841.       add_number -= size + where + fragP -> fr_address;
  842.       if (add_symbolP == 0)
  843.         {
  844.           fixP -> fx_addsy = & abs_symbol;
  845.           seg_reloc_count ++;
  846.         }
  847.     }
  848.       md_number_to_chars (place, add_number, size);
  849.     }                /* For each fixS in this segment. */
  850.   return (seg_reloc_count);
  851. }                /* fixup_segment() */
  852.  
  853. /*
  854.  *        emit_relocations()
  855.  *
  856.  * Crawl along a fixS chain. Emit the segment's recocations.
  857.  */
  858. static void
  859. emit_relocations (fixP, segment_address_in_file)
  860.      register fixS *    fixP;    /* Fixup chain for this segment. */
  861.      relax_addressT    segment_address_in_file;
  862. {
  863.   struct relocation_info    ri;
  864.   register symbolS *        symbolP;
  865.  
  866.     /* JF this is for paranoia */
  867.   bzero((char *)&ri,sizeof(ri));
  868.   for ( ;  fixP;  fixP = fixP -> fx_next)
  869.     {
  870.       if (symbolP = fixP -> fx_addsy)
  871.     {
  872.       ri . r_length        = nbytes_r_length [fixP -> fx_size];
  873.       ri . r_pcrel        = fixP -> fx_pcrel;
  874.       ri . r_address    = fixP -> fx_frag -> fr_address
  875.         +   fixP -> fx_where
  876.           - segment_address_in_file;
  877.       if ((symbolP -> sy_type & N_TYPE) == N_UNDF)
  878.         {
  879.           ri . r_extern    = 1;
  880.           ri . r_symbolnum    = symbolP -> sy_number;
  881.         }
  882.       else
  883.         {
  884.           ri . r_extern    = 0;
  885.           ri . r_symbolnum    = symbolP -> sy_type & N_TYPE;
  886.         }
  887.       append (&next_object_file_charP, (char *)& ri, (unsigned long)sizeof(ri));
  888.     }
  889.     }
  890.  
  891. }
  892.  
  893. isdnrange(f1,f2)
  894. struct frag *f1,*f2;
  895. {
  896.     while(f1) {
  897.         if(f1->fr_next==f2)
  898.             return 1;
  899.         f1=f1->fr_next;
  900.     }
  901.     return 0;
  902. }
  903. /* End: as-write.c */
  904.